package com.yzc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.beans.factory.annotation.Value;

import java.util.Properties;

@Configuration
public class EmailConfig {

    @Value("${spring.mail.host}")
    private String mailHost;

    @Value("${spring.mail.port}")
    private int mailPort;

    @Value("${spring.mail.username}")
    private String mailUsername;

    @Value("${spring.mail.password}")
    private String mailPassword; // 授权码

    @Bean
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(mailHost);  // 邮件服务器地址
        mailSender.setPort(mailPort);  // 邮件服务器端口

        mailSender.setUsername(mailUsername); // 发件人邮箱
        mailSender.setPassword(mailPassword); // 授权码

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.smtp.auth", "true");         // 启用SMTP认证
        props.put("mail.smtp.starttls.enable", "true"); // 启用STARTTLS加密

        return mailSender;
    }
}
